package com.mldong.interceptor;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.mldong.holder.LoginUserHolder;
import com.mldong.modules.sys.enums.UserAdminTypeEnum;
import com.mldong.modules.sys.vo.LoginVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MySaInterceptor extends SaInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 校验是否登录
        StpUtil.checkLogin();
        LoginVO vo = BeanUtil.toBean(StpUtil.getExtra(LoginVO.class.getSimpleName()),LoginVO.class);
        // 给当前线程绑定用户信息
        LoginUserHolder.set(vo);
        UserAdminTypeEnum userType = vo.getAdminType();
        // 校验是否超级管理员
        if(UserAdminTypeEnum.ADMIN.equals(userType))  return true;
        // 校验注解逻辑
        return super.preHandle(request, response, handler);
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        LoginUserHolder.remove();
    }
}
